<?php

class viewEmploymentContractTypeListAction extends sfAction
{

    private $employmentContractTypeService;

    public function getEmploymentContractTypeService()
    {
        if (is_null($this->employmentContractTypeService)) {
            $this->employmentContractTypeService = new EmploymentContractTypeService();
            $this->employmentContractTypeService->setEmploymentContractTypeDao(new EmploymentContractTypeDao());
        }
        return $this->employmentContractTypeService;
    }

    public function execute($request)
    {

        $usrObj = $this->getUser()->getAttribute('user');
        if (!($usrObj->isAdmin())) {
            $this->redirect('pim/viewPersonalDetails');
        }

        $employmentContractTypeId = $request->getParameter('employmentContractTypeId');
        $isPaging = $request->getParameter('pageNo');

        $pageNumber = $isPaging;
        if (!empty($employmentContractTypeId) && $this->getUser()->hasAttribute('pageNumber')) {
            $pageNumber = $this->getUser()->getAttribute('pageNumber');
        }

        $sortField = $request->getParameter('sortField');
        $sortOrder = $request->getParameter('sortOrder');

        $noOfRecords = EmploymentContractType::NO_OF_RECORDS_PER_PAGE;
        $offset = ($pageNumber >= 1) ? (($pageNumber - 1) * $noOfRecords) : ($request->getParameter('pageNo', 1) - 1) * $noOfRecords;

        $EmploymentContractTypeList = $this->getEmploymentContractTypeService()->getEmploymentContractTypeList($sortField, $sortOrder, true, $noOfRecords, $offset);
        $this->_setListComponent($EmploymentContractTypeList, $noOfRecords, $pageNumber);
        $this->getUser()->setAttribute('pageNumber', $pageNumber);
        $params = array();
        $this->parmetersForListCompoment = $params;
    }

    private function _setListComponent($EmploymentContractTypeList, $noOfRecords, $pageNumber)
    {

        $configurationFactory = new EmploymentContractTypeHeaderFactory();
        ohrmListComponent::setConfigurationFactory($configurationFactory);
        ohrmListComponent::setListData($EmploymentContractTypeList);
        ohrmListComponent::setPageNumber($pageNumber);
        ohrmListComponent::setItemsPerPage($noOfRecords);
        ohrmListComponent::setNumberOfRecords(count($this->getEmploymentContractTypeService()->getEmploymentContractTypeList()));
    }

}